package com.sale.taskplugins;

import com.apache.api.api.TimeTaskService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.tools.DateUtils;
import com.apache.workday.WorkdayUtils;
import org.apache.log4j.Logger;

import java.util.Date;

/**
 * 【开市初始化行情】-每天早上开市初始化行情（开盘价，其他行情数据等）
 *
 * @author Administrator 创建时间：2017-2-15
 */
public class InitQuotationTaskPluginImpl implements PluginConnector, TimeTaskService {

    private ProxyManager netProxyManager;

    private IDao trpFeeDao;

    private transient static boolean CancellationsTask = true;

    private Logger log = Logger.getLogger(getClass());

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
     */
    public Object execute(ParamsVo arg0) throws Exception {
        exeRuning();
        return new ResultEntity();
    }

    public boolean run() {
        exeRuning();
        return true;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void exeRuning() {
        try {
            if (CancellationsTask) {
                CancellationsTask = false;
                ParamsVo vo = new ParamsVo();

                Date date = new Date();//默认等于当日
                //判断执行日期是否是工作日（交易日）
                String extDate = DateUtils.format(date, "yyyyMMdd");
                boolean isWorkDay = WorkdayUtils.getInstance().isWorkday(extDate);
                if (!isWorkDay) {
                    log.info(extDate + "不是交易日，定时器不需执行[初始化开盘价]");
                } else {
                    //是交易日即开始初始化开盘价
                    log.info("初始化[" + extDate + "]开盘价 start！！");
                    //获取指定日期前一天的数据
                    String lastDate = DateUtils
                            .format(DateUtils.beforeNDays(date, 1), "yyyy-MM-dd");
                    log.info("本次以[" + lastDate + "]的数据为初始化[" + extDate + "]的基准！");
                    if (Validator.isNotNull(lastDate)) {
                        MethodParam params = new MethodParam("OpenPrice", "", "",
                                "com.dwusoft.net.trans.TransPublic");
                        params.setParams("lastDate", lastDate);
                        if (Validator.isEmpty(trpFeeDao)) {
                            trpFeeDao = (IDao) SpringContextLoader.getBean("trpFeeDao");
                        }
                        trpFeeDao.select(params);
                    }
                    log.info("初始化[" + extDate + "]开盘价  end！！");
                }
            }
        } finally {
            CancellationsTask = true;
        }
    }

    public void setNetProxyManager(ProxyManager netProxyManager) {
        this.netProxyManager = netProxyManager;
    }

    public void setTrpFeeDao(IDao trpFeeDao) {
        this.trpFeeDao = trpFeeDao;
    }

}
